//
//  MessageTipsCell.m
//  BaiTengIM
//
//  Created by awh on 2019/7/16.
//  Copyright © 2019 awh. All rights reserved.
//

#import "MessageTipsCell.h"
#import "BTMessageModel.h"

@interface MessageTipsCell ()

@property(nonatomic, strong) UILabel *contentLabel;

@end

@implementation MessageTipsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    _contentLabel = [UILabel new];
    _contentLabel.textColor = [UIColor grayColor];
    _contentLabel.font = [UIFont systemFontOfSize:13];
    _contentLabel.textAlignment = NSTextAlignmentCenter;
    _contentLabel.backgroundColor = self.contentView.backgroundColor = self.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_contentLabel];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(5);
        make.left.equalTo(self.contentView).offset(20);
        make.right.equalTo(self.contentView).offset(-20);
        make.bottom.equalTo(self.contentView).offset(-5);
    }];
}

- (void)setMessage:(BTMessageModel *)message {
   _message = message;
    if (message.msgtype == MessageTypeRecall) { // 消息撤回提醒
        if (message.messageDirection == MessageDirectionSend) {
            self.contentLabel.text =  @"你撤回了一条消息";
        } else {
            if ([message.type isEqualToString:@"friend"]) {
                self.contentLabel.text = @"对方撤回了一条消息";
            } else {
                // 内容
//                ContactorModel *groupMember = [[LoginManager shareLoginManager].userDataBase ReadGroupUserInfoWithGroupID:[NSString stringWithFormat:@"%ld", (long)message.groupId] UserID:[NSString stringWithFormat:@"%ld", (long)message.uid]];
//                self.contentLabel.text =[NSString stringWithFormat:@"%@撤回了一条消息", groupMember.memberModel.nickName];
            }
        }
    } else if (message.msgtype == MessageTypeTime) {
        self.contentLabel.text = [NSDate dateStrFromCstampTime:message.timestamp  withDateFormat:@"yyyy-MM-dd HH:mm"];
    } else {
        self.contentLabel.text=[NSString stringWithFormat:@"%@",message.msg];
    }
    
    if ([self.contentLabel.text  containsString:@"重新添加好友"]) {
        NSString * str =@"添加好友";
        NSMutableAttributedString * att =[[NSMutableAttributedString alloc]initWithString:self.contentLabel.text];
        [att addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(self.contentLabel.text.length-str.length,str.length)];
        self.contentLabel.attributedText= att;
        [self.contentLabel addTapCallBack:self sel:@selector(didClickAddFriends)];
    }else
    {
        self.contentLabel.userInteractionEnabled=NO;
    }
    
}
-(void)didClickAddFriends
{
    if ([self.delegate respondsToSelector:@selector(messageTipsCellDidSelected:)]) {
        [self.delegate messageTipsCellDidSelected:self];
    }
}
@end
